<template>
  <div class="rankings-view" v-if="list.total > 0">
    <ul class="info-list">
      <list-template
        v-for="(item, index) of list.rows"
        :key="item.bookId"
        :item="item"
        :diff="'rankings-list'"
        :index="index + 1"
      />
    </ul>
    <ul class="pagination-box" v-show="list.pages > 1">
      <li class="pre-page">
        <mt-button @click.native="prePage">
          <svg-icon class="preIcon" icon-class="preIcon" slot="icon"></svg-icon>
          Previous page
        </mt-button>
      </li>
      <li class="cur-total">
        <span class="current" v-text="list.current"></span>
        /
        <span class="total" v-text="list.pages"></span>
      </li>
      <li class="next-page">
        <mt-button @click.native="nextPage">
          Next page
          <svg-icon
            class="nextIcon"
            icon-class="nextIcon"
            slot="icon"
          ></svg-icon>
        </mt-button>
      </li>
    </ul>
  </div>
  <div class="empty" v-else-if="list.total == 0">
    <span>No resources available</span>
  </div>
</template>

<script>
import listTemplate from "@/components/common/listTemplate/Index";
export default {
  data() {
    return {
      list: {}, //书籍列表
      params: {
        pageNum: 1,
        pageSize: 20,
        id: this.$route.params.title,
      },
      scrollTop: 0,
    };
  },
  components: {
    listTemplate,
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    if (this.$route.params.title !== "null") {
      this.rankings();
    }
  },
  methods: {
    // 上一页
    prePage() {
      if (this.list.current > 1) {
        this.params.pageNum -= 1;
        this.rankings();
        this.backTop();
      } else {
        return;
      }
    },
    // 下一页
    nextPage() {
      if (this.list.current < this.list.pages) {
        this.params.pageNum += 1;
        this.rankings();
        this.backTop();
      } else {
        return;
      }
    },
    async rankings() {
      try {
        let res = await this.$api.home.rankings(this.params);
        if (res.code === 1) {
          this.list = res.source;
        }
      } catch (e) {
        console.log(e);
      }
    },
    backTop() {
      let timer = setInterval(() => {
        let ispeed = Math.floor(-this.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          this.scrollTop + ispeed;
        if (this.scrollTop === 0) {
          clearInterval(timer);
        }
      }, 160);
    },
  },
};
</script>

<style lang="scss" scoped>
.rankings-view {
  padding: 40px 0 20px 0;
  .info-list {
  }
  .pagination-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .pre-page,
    .next-page {
      width: 208px;
      height: 58px;
      .mint-button {
        width: 100%;
        height: 100%;
        padding: 0;
        display: flex;
        align-items: center;
        justify-content: space-around;
        background: #e86472;
        border-radius: 4px;
        font-weight: 600;
        color: #fff;
        font-size: 22px;
        .mint-button-icon {
          .svg-icon {
            width: 11px;
            height: 24px;
          }
        }
      }
      &.next-page {
        .mint-button {
          flex-direction: row-reverse;
        }
      }
    }
    .cur-total {
      color: #999999;
      font-size: 20px;
    }
  }
}
.empty {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>
